package org.flashbrighton.as3bots.logic
{
	import org.flashbrighton.as3bots.events.BotEvent;
	
	public class NeilsBotLogic implements IBotLogic
	{		
		// The most amount of time (in frames) 
		// before the bot decides another action.
		private const MAX_TIME:int = 120;
		
		private var _engineVal:Number;
		private var _rotationVal:Number;
		
		// The counter that controls the engine's active phases
		private var mECount:int;
		private var mEMax:int;
		
		// The duration (frames) over which the _engineVal will be applied
		private var mEDuration:int;
		
		// The duration (frames) over which the mRotationValue will be applied
		private var  mRDuration:int;
		
		// The counter that controls the rotator's active phases
		private var mRCount:int;
		private var mRMax:int;
		
		
		/**
		 * Constructor.
		 */
		public function NeilsBotLogic()
		{
			resetEngine();
			resetRotation();
		}
		
		
		/**
		 * The 'thinking' code for the bot.
		 * Code here is run every frame.
		 */
		public function run():void
		{
			callEngine();
			callRotator();
		}
		
		
		/**
		 * The engine value sent to the game.
		 */
		public function notify(event:BotEvent):void
		{
			
			if (event.type == BotEvent.HIT)
			{

				// reverses bot		
				mECount = 0;
				mRCount = 0;
				_engineVal = -_engineVal;
				_rotationVal = 0			
				mEDuration = 5;			
				mRDuration = 5
				mEMax = mEDuration;
				mRMax = mRDuration;
				
			}
		}
		
		
		/**
		 * The engine value sent to the game.
		 */
		public function get enginePercent():Number
		{
			return _engineVal;
		}
		
		
		/**
		 * The rotation value sent to the game.
		 */
		public function get rotatorPercent():Number
		{
			return _rotationVal;
		}
		
		
		private function resetEngine():void
		{
			// Set the count to zero
			mECount = 0;
			
			// Set the duration of the count to a random value
			mEMax = randomRange(30, MAX_TIME);
			
			// Set the duration of the engine to a random value less than the mECount value	
			mEDuration = randomRange(10, mEMax);
			
			// Set the value of the engine to a random value
			_engineVal = randomRange(-1, 1);
		}
		
		
		private function resetRotation():void
		{
			// Set the count to zero
			mRCount = 0;
			
			// Set the duration of the count to a random value
			mRMax = randomRange(30, MAX_TIME);
			
			// Set the duration of the rotation to a random value less than the mRCount value	
			mRDuration = randomRange(10, mRMax);
			
			// Set the value of the rotation to a random value
			_rotationVal = randomRange(-1, 1);
		}
		
		
		private function callEngine():void
		{
			if(mECount < mEMax)
			{
				// Still counting
				mECount++;				
			}
			else
			{	
				// Reset counting
				resetEngine();
			}
		}
		
		
		private function callRotator():void
		{
			if(mRCount < mRMax)
			{
				// Still counting
				mRCount++;
			}
			else
			{	
				// Reset counting
				resetRotation();
			}
		}
		
		
		private function randomRange(pMin:Number, pMax:Number):Number
		{
			return (pMin + Math.random() * (pMax - pMin));
   	}
		
	}
}